/*
 * @lc app=leetcode.cn id=219 lang=javascript
 *
 * [219] 存在重复元素 II
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var containsNearbyDuplicate = function(nums, k) {   
  let s=new Set()
  let len=nums.length
  for(let i=0;i<len;i++){
      if(s.has(nums[i])){
          return true
      }
        s.add(nums[i])
      if(s.size>k){
          s.delete(nums[i-k])
      }
  }
  return false
};
// @lc code=end

var containsNearbyDuplicate = function(nums, k) {   
    let len=nums.length
    for(let i=0;i<len;i++){
        for(let j=i+1;j<=i+k;j++){
            if(nums[i]===nums[j])return true
        }
    }
    return false
};//BF解法
containsNearbyDuplicate([1,2,3,1,2,3],2)